package com.kitty.game.equip.handler;

import static com.kitty.game.utils.Const.MONEY_NAME;
import static com.kitty.game.utils.Const.ROLE_RESOURCE_NOT_ENOUGH;

import com.kitty.game.config.Equip;
import com.kitty.game.enter.RespGeneralNotify;
import com.kitty.game.equip.EquipDataPool;
import com.kitty.game.equip.message.RespHunQiFenHuaRes;
import com.kitty.game.equip.model.HunQiAttrField;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.role.model.Role;
import com.kitty.game.utils.NotifyModules;
import com.kitty.game.utils.StringUtil;
import com.kitty.mina.message.MessagePusher;
import java.text.MessageFormat;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 魂器 进化
 */
@Component("EquipHunQiEvolveHandler")
@Slf4j
public class EquipHunQiEvolveHandler extends UpgradeHandler {

    public static final String EXCEPTION = "失败";

    public static final String FAIL = "魂器进化后等级不可超过角色等级";
    /**
     * 炼化成功提示
     */
    private static final String REFINE_SUCCESS = "恭喜你，炼化成功。";

    /**
     * 炼化失败提示
     */
    private static final String REFINE_FAIL = "真遗憾，炼化失败了。";

    private static final Short ATTR_TYPE = 260;

    @Override
    public String doUpgrade(Role role, Object... params) {
        RoleEquip roleEquip = (RoleEquip) params[0];
        String param = (String) params[1];
        Equip equip = (Equip) params[2];

        boolean success = true;

        short targetLevel = Short.parseShort(param);
        short level = (short) (targetLevel - roleEquip.queryLevel());

        /**扣钱*/
        int cash = needCash(level, 0);
        roleService.subtractMoney(role, cash);

        if (success) {
            roleEquip.alterEvolveLevel(Short.parseShort(param));

            refreshRoleEquip(role, roleEquip);
            //不计算完美度
            //equipService.countPerfect(roleEquip);
            RespGeneralNotify respGeneralNotify = new RespGeneralNotify();
            respGeneralNotify.setValue(roleEquip.getId() + "");
            respGeneralNotify.setNotify((short) NotifyModules.NOTIFY_EQUIP_REFINE_OK);//炼化成功
            MessagePusher.pushMessage(role, respGeneralNotify);
            roleService.addAchieveScore(role,"304003");
            pushOperateResult(role, success);
            return REFINE_SUCCESS;
        } else {
            pushOperateResult(role, success);
            return REFINE_FAIL;
        }
    }

    @Override
    public String acceptUpgrade(Role role, int upgradePos, String param) {
        RoleEquip roleEquip = equipService.getRoleEquipByPos(role, upgradePos);
        if (roleEquip == null) {
            return "请放入正确的装备。";
        }
        if (!isRightPosition(roleEquip.queryEquipPos(false))) {
            return MessageFormat.format(NO_RIGHT_POSITION, "魂器进化", roleEquip.getName());
        }
        Equip equip = EquipDataPool.getByName(roleEquip.getName());
        if (equip == null) {
            log.error("找不到装备配置ICON=[{}]", upgradePos);
            return MessageFormat.format(NOT_FIND_CONFIG, roleEquip.getName());
        }

        String noEnough = checkResource(role, param, roleEquip);
        if (noEnough != null) {
            return noEnough;
        }
        return doUpgrade(role, roleEquip, param, equip);
    }

    @Override
    public String checkResource(Object... objects) {
        Role role = (Role) objects[0];
        String param = (String) objects[1];
        int toLevel = Integer.parseInt(param);
        RoleEquip roleEquip = (RoleEquip) objects[2];
        if (toLevel > roleEquip.queryLevel()){
            if (!roleEquip.getName().startsWith("魂器")) {
                return EXCEPTION;
            }

            short targetLevel = Short.parseShort(param);
            short level = (short) (targetLevel - roleEquip.queryLevel());
            if (level <= 0) {
                return FAIL;
            }

            if (needCash(level, 0) > role.getMoney()) {
                return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, MONEY_NAME);
            }
            if (toLevel > role.getLevel()){
                return "魂器进化等级不能超过人物等级";
            }
        }else {

            if (roleEquip.queryLevel() - toLevel> 20){
                return "每次最多退化20级";
            }
            if (role.getLevel() >  toLevel){
                return "退化等级不能低于人物等级";
            }
        }
        return null;
    }


    @Override
    public int needCash(short level, int pos) {
        //之前生成的魂器 默认等级是1 现在改成75 做个进化的兼容
        return (level >= 75 ? level - 74 : level) * 500 * 10000;
    }

    @Override
    public String getUpdateTypeStr() {
        return null;
    }
}
